home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / FREENET / MELL / NETLIB00 / NetLib / c / pton < prev    next >
Text File  |  1996-06-27  |  5KB  |  194 lines

  1. /* Copyright (c) 1996 by Internet Software Consortium.
  2.  *
  3.  * Permission to use, copy, modify, and distribute this software for any
  4.  * purpose with or without fee is hereby granted, provided that the above
  5.  * copyright notice and this permission notice appear in all copies.
  6.  *
  7.  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  8.  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  9.  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  10.  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  12.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  13.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14.  * SOFTWARE.
  15.  */
  16.  
  17. #include "netinet/in.h"
  18. #include "arpa/inet.h"
  19. #include "Internet:sys.h.byteorder"
  20. #include <ctype.h>
  21. #include "arpa/inet.h"
  22. #include "arpa/nameser.h"
  23. #include <string.h>
  24. #include "sys/errno.h"
  25. #include "sys/socket.h"
  26. #include <stdio.h>
  27.  
  28. /* int
  29.  * inet_pton4(src, dst)
  30.  *    like inet_aton() but without all the hexadecimal and shorthand.
  31.  * return:
  32.  *    1 if `src' is a valid dotted quad, else 0.
  33.  * notice:
  34.  *    does not touch `dst' unless it's returning 1.
  35.  * author:
  36.  *    Paul Vixie, 1996.
  37.  */
  38. static int inet_pton4(const char *src, u_char *dst)
  39. {
  40.     static const char digits[] = "0123456789";
  41.     int saw_digit, octets, ch;
  42.     u_char tmp[INADDRSZ], *tp;
  43.  
  44.     saw_digit = 0;
  45.     octets = 0;
  46.     *(tp = tmp) = 0;
  47.     while ((ch = *src++) != '\0') {
  48.         const char *pch;
  49.  
  50.         if ((pch = strchr(digits, ch)) != NULL) {
  51.             u_int new = *tp * 10 + (pch - digits);
  52.  
  53.             if (new > 255)
  54.                 return (0);
  55.             *tp = new;
  56.             if (! saw_digit) {
  57.                 if (++octets > 4)
  58.                     return (0);
  59.                 saw_digit = 1;
  60.             }
  61.         } else if (ch == '.' && saw_digit) {
  62.             if (octets == 4)
  63.                 return (0);
  64.             *++tp = 0;
  65.             saw_digit = 0;
  66.         } else
  67.             return (0);
  68.     }
  69.     if (octets < 4)
  70.         return (0);
  71.     memcpy(dst, tmp, INADDRSZ);
  72.     return (1);
  73. }
  74.  
  75. /* int
  76.  * inet_pton6(src, dst)
  77.  *    convert presentation level address to network order binary form.
  78.  * return:
  79.  *    1 if `src' is a valid [RFC1884 2.2] address, else 0.
  80.  * notice:
  81.  *    (1) does not touch `dst' unless it's returning 1.
  82.  *    (2) :: in a full address is silently ignored.
  83.  * credit:
  84.  *    inspired by Mark Andrews.
  85.  * author:
  86.  *    Paul Vixie, 1996.
  87.  */
  88. static int inet_pton6(const char *src, u_char *dst)
  89. {
  90.     static const char xdigits_l[] = "0123456789abcdef",
  91.               xdigits_u[] = "0123456789ABCDEF";
  92.     u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp;
  93.     const char *xdigits, *curtok;
  94.     int ch, saw_xdigit;
  95.     u_int val;
  96.  
  97.     memset((tp = tmp), 0, IN6ADDRSZ);
  98.     endp = tp + IN6ADDRSZ;
  99.     colonp = NULL;
  100.     /* Leading :: requires some special handling. */
  101.     if (*src == ':')
  102.         if (*++src != ':')
  103.             return (0);
  104.     curtok = src;
  105.     saw_xdigit = 0;
  106.     val = 0;
  107.     while ((ch = *src++) != '\0') {
  108.         const char *pch;
  109.  
  110.         if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
  111.             pch = strchr((xdigits = xdigits_u), ch);
  112.         if (pch != NULL) {
  113.             val <<= 4;
  114.             val |= (pch - xdigits);
  115.             if (val > 0xffff)
  116.                 return (0);
  117.             saw_xdigit = 1;
  118.             continue;
  119.         }
  120.         if (ch == ':') {
  121.             curtok = src;
  122.             if (!saw_xdigit) {
  123.                 if (colonp)
  124.                     return (0);
  125.                 colonp = tp;
  126.                 continue;
  127.             }
  128.             if (tp + INT16SZ > endp)
  129.                 return (0);
  130.             *tp++ = (u_char) (val >> 8) & 0xff;
  131.             *tp++ = (u_char) val & 0xff;
  132.             saw_xdigit = 0;
  133.             val = 0;
  134.             continue;
  135.         }
  136.         if (ch == '.' && ((tp + INADDRSZ) <= endp) &&
  137.             inet_pton4(curtok, tp) > 0) {
  138.             tp += INADDRSZ;
  139.             saw_xdigit = 0;
  140.             break;    /* '\0' was seen by inet_pton4(). */
  141.         }
  142.         return (0);
  143.     }
  144.     if (saw_xdigit) {
  145.         if (tp + INT16SZ > endp)
  146.             return (0);
  147.         *tp++ = (u_char) (val >> 8) & 0xff;
  148.         *tp++ = (u_char) val & 0xff;
  149.     }
  150.     if (colonp != NULL) {
  151.         /*
  152.          * Since some memmove()'s erroneously fail to handle
  153.          * overlapping regions, we'll do the shift by hand.
  154.          */
  155.         const int n = tp - colonp;
  156.         int i;
  157.  
  158.         for (i = 1; i <= n; i++) {
  159.             endp[- i] = colonp[n - i];
  160.             colonp[n - i] = 0;
  161.         }
  162.         tp = endp;
  163.     }
  164.     if (tp != endp)
  165.         return (0);
  166.     memcpy(dst, tmp, IN6ADDRSZ);
  167.     return (1);
  168. }
  169.  
  170. /* int
  171.  * inet_pton(af, src, dst)
  172.  *    convert from presentation format (which usually means ASCII printable)
  173.  *    to network format (which is usually some kind of binary format).
  174.  * return:
  175.  *    1 if the address was valid for the specified address family
  176.  *    0 if the address wasn't valid (`dst' is untouched in this case)
  177.  *    -1 if some other error occurred (`dst' is untouched in this case, too)
  178.  * author:
  179.  *    Paul Vixie, 1996.
  180.  */
  181. int inet_pton(int af, const char *src, void *dst)
  182. {
  183.     switch (af) {
  184.     case AF_INET:
  185.         return (inet_pton4(src, dst));
  186.     case AF_INET6:
  187.         return (inet_pton6(src, dst));
  188.     default:
  189.         errno = EAFNOSUPPORT;
  190.         return (-1);
  191.     }
  192.     /* NOTREACHED */
  193. }
  194.